package com.atguigu.gmall.list.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.list.service.ListSerivce;
import com.atguigu.gmall.model.list.Goods;
import com.atguigu.gmall.model.list.SearchParam;
import com.atguigu.gmall.model.list.SearchResponseVo;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.transport.TransportClient;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/api/list")
public class ListApiController {

    //ES客户端 RestHighLevelClient高级  TransportClient低级
    @Resource
    private RestHighLevelClient restHighLevelClient;
/*
    @Resource
    private TransportClient transportClient;
*/
    //SpringDataES封装后  ElasticsearchRestTemplate高级  ElasticsearchTemplate低级
    @Resource
    private ElasticsearchRestTemplate elasticsearchRestTemplate;
/*
    @Resource
    private ElasticsearchTemplate elasticsearchTemplate;
*/

    @Resource
    private ListSerivce listSerivce;

    //创建索引
    @GetMapping("/creatIndex")
    public Result createIndex(){
        //创建索引
        elasticsearchRestTemplate.createIndex(Goods.class);//goods类中属性上注释已设置映射
        elasticsearchRestTemplate.putMapping(Goods.class);
        return Result.ok();
    }
    //导入索引：sku上架时保存到ES中时的内容
    @GetMapping("/upperGoods/{skuId}")
    public Result upperGoods(@PathVariable Long skuId){
        listSerivce.upperGoods(skuId);
        return Result.ok();
    }
    //删除索引
    @GetMapping("/lowerGoods/{skuId}")
    public Result lowerGoods(@PathVariable Long skuId){
        listSerivce.lowerGoods(skuId);
        return Result.ok();
    }

    //增加热度
    @GetMapping("/hotScore/{skuId}")
    public Result hotScore(@PathVariable Long skuId){//数据
        listSerivce.hotScore(skuId);
        return Result.ok();
    }

    //开始搜索ES索引库
    @PostMapping("/search")
    public SearchResponseVo search(@RequestBody  SearchParam searchParam){//对象
        return listSerivce.search(searchParam);
    }
}
